
import Vue from 'vue'

const state = () => ({
  // 站类
  stationTypeList: [],
  // 流域分区
  watershedZones: [],
  // 行政区域
  adminAreas: [],
  // 当前激活图层(layerId),无则全显示
  visibleLayerId: '',
  // 地图站点查询参数
  mapStationQueryParam: {
    SWADDVCD: '130000',
    SWBSNM: '',
    STTP: '', // 水文站类
    SQADDVCD: '130000',
    SQBSNM: '',
    SEARCH_TIME: '2024-03-01'// 水质站时间
  }
})

const mutations = {
  setStationTypeList (state, list) {
    state.stationTypeList = list
  },
  setWatershedZones (state, list) {
    state.watershedZones = list
  },
  setAdminAreas (state, list) {
    state.adminAreas = list
  },
  setVisibleLayerId (state, id) {
    state.visibleLayerId = id
  },
  setMapStationQueryParam (state, params) {
    state.mapStationQueryParam = {
      ...state.mapStationQueryParam,
      ...params
    }
  }
}

const actions = {
  async getStationTypeList ({ commit }) {
    const rows = await Vue.prototype.$utils.dictionary.getOptionsByKey('YSJC_UW_SWSWZ')
    commit('setStationTypeList', rows)
  },
  async getWatershedZones ({ commit }) {
    const { rows } = await Vue.prototype.$request.singleTableOperation({
      url: '/grid/getTableData',
      metaSet: 'YSQJC_ST_STBPRP_B',
      methodCode: 'Bsnm_GetList',
      methodParams: ''
    })
    commit('setWatershedZones', rows.map(item => {
      return {
        ...item,
        label: item.BSNM,
        value: item.BSNM
      }
    }))
  },
  async getAdminAreas ({ commit }) {
    // const rows = await Vue.prototype.$utils.dictionary.getOptionsByKey('YSQJC_JC_XZQ')
    const { rows } = await Vue.prototype.$request.singleTableOperation({
      url: '/grid/getTableData',
      metaSet: 'YSQJC_JC_XZQ',
      methodCode: 'get_sjxzq',
      methodParams: ''
    })
    function listToTree (id) {
      const arr = rows.filter(item => item.FXZQDM === id)
      if (!arr.length) return null
      return arr.map(item => ({
        label: item.XZQMC,
        value: item.XZQDM,
        children: listToTree(item.XZQDM)
      }))
    }
    commit('setAdminAreas', listToTree('ROOT'))
  }
}

export default { state, mutations, actions }
